package com.telecom.at.front;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.telecom.at.db.dac.Dac;
import com.telecom.at.db.model.Doc;

@WebServlet("/front/DelDoc")
public class DelDoc extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        long id = Long.parseLong(request.getParameter("id"));
        Doc doc = Dac.getInstance().getDoc(id);
        if (Dac.getInstance().delDoc(id)) {
            String fileFullName = getServletContext().getRealPath(doc.getFullName());
            new File(fileFullName).delete();
            response.sendRedirect("ListDoc");
        } else {
            request.setAttribute("errMsg", "删除文档失败");
            request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
